﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using ShapeDemo.UC.WinProp;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;
using ShapeDemo.UC;

namespace ShapeDemo
{

    public class PolygonDrawer
    {
        public Canvas PolygonCanvas { get; set; }
        

        public List<ASPNETX_WPFPolygon> PolygonCollection { get; set; }

        private WinPropPolygon winPolygonProp;
        private WinPropCommon winCommonProp;
        

        ASPNETX_WPFPolygon pgBody;

        private TMouse _MouseType;
        
        public TMouse MouseTye
        {
            get
            {
                return _MouseType;
            }
            set
            {
                switch (value)
                { 
                    case TMouse.Drawing:

                        PolygonCanvas.Cursor = Cursors.Cross;

                        foreach (ASPNETX_WPFPolygon item in PolygonCollection)
                        {
                            item.PolygonBody.Cursor = Cursors.Cross;
                        }

                        _MouseType = value;

                        break;
                    case TMouse.Point:

                        PolygonCanvas.Cursor = Cursors.Arrow;

                        foreach (ASPNETX_WPFPolygon item in PolygonCollection)
                        {
                            item.PolygonBody.Cursor = Cursors.Hand;
                            item.PolygonBody.MouseLeftButtonDown += new MouseButtonEventHandler(PolygonBody_MouseLeftButtonDown);
                        }

                        _MouseType = value;

                        break;
                }
            }
        }

        private ContextMenu cMenu = new ContextMenu();


        public ucTooltip PgnTooltip { get; set; }

        #region TOOLTIP METHODS

        public void HideTooltipEvt(object sender, MouseEventArgs e)
        {
            Polygon polygon = ((Polygon)sender);
            polygon.Cursor = Cursors.Arrow;
            PgnTooltip.Hide();
        }


        public void MoveTooltipEvt(object sender, MouseEventArgs e)
        {
            Polygon polygon = ((Polygon)sender);
            PgnTooltip.Show(e.GetPosition(PolygonCanvas));
            
        }

        public void ShowTooltipEvt(object sender, MouseEventArgs e)
        {
            Polygon polygon = ((Polygon)sender);
            ASPNETX_WPFPolygon pTemp = PolygonCollection.First<ASPNETX_WPFPolygon>(p => p.PolygonBody.Name == polygon.Name);
            PgnTooltip.Title = pTemp.CommonProperty.Content;
            PgnTooltip.Description = pTemp.CommonProperty.Detail;
            polygon.Cursor = Cursors.None;
        }
        
        #endregion

        public PolygonDrawer(Canvas canvasContent, WinPropPolygon winShapeProp,WinPropCommon winProp)
        {
            PolygonCollection = new List<ASPNETX_WPFPolygon>();
            
            PolygonCanvas = canvasContent;
            winPolygonProp = winShapeProp;
            winCommonProp = winProp;

            _MouseType = TMouse.Drawing;

            MenuItem miFinish = new MenuItem();
            miFinish.Header = "Finalizar";
            miFinish.Click += new RoutedEventHandler(MenuItem_Finish_Click);

            MenuItem miDel = new MenuItem();
            miDel.Header = "Borrar el ultimo punto";
            miDel.Click += new RoutedEventHandler(MenuItem_DelLastPoint_Click);

            MenuItem miClear = new MenuItem();
            miClear.Header = "Limpiar";
            miClear.Click += new RoutedEventHandler(MenuItem_Redraw_Click);

            cMenu.Items.Add(miFinish);
            cMenu.Items.Add(miDel);
            cMenu.Items.Add(miClear);

            PolygonCanvas.ContextMenu = cMenu;

            PolygonCanvas.MouseLeftButtonDown += new System.Windows.Input.MouseButtonEventHandler(polygonCanvas_MouseLeftButtonDown);
            PolygonCanvas.MouseMove += new System.Windows.Input.MouseEventHandler(polygonCanvas_MouseMove);

            BindListBox();
        }

        public void Draw()
        {
            pgBody = ResourceCenter.NewPolygon();
            PolygonCanvas.Children.Add(pgBody.PolygonBody);
        }


        void polygonCanvas_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (_MouseType == TMouse.Drawing)
            {
                if (pgBody.PolygonBody.Points.Count > 0)
                {
                    Point p = e.GetPosition(PolygonCanvas);
                    int pgPointCount = pgBody.PolygonBody.Points.Count;

                    pgBody.PolygonBody.Points[pgPointCount - 1] = p;

                    winPolygonProp.X = p.X;
                    winPolygonProp.Y = p.Y;
                }
            }
        }

        void polygonCanvas_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (_MouseType == TMouse.Drawing)
            {
                Point p = e.GetPosition(PolygonCanvas);


                int pgPointCount = pgBody.PolygonBody.Points.Count;
                pgBody.PolygonBody.Points[pgPointCount - 1] = p;
                pgBody.PolygonBody.Points.Add(p);

                winPolygonProp.lstPointsList.Items.Add("(" + p.X + "," + p.Y + ")");
            }
        }

        public void BindListBox()
        {
            winCommonProp.lstHistory.Items.Clear();
            foreach (ASPNETX_WPFPolygon item in PolygonCollection)
            {
                winCommonProp.lstHistory.Items.Add(item.CommonProperty.Content);
            }
        }

        private void MenuItem_Finish_Click(object sender, RoutedEventArgs e)
        {
            if (pgBody.PolygonBody.Points.Count > 2)
            {

                pgBody.PolygonBody.Points.RemoveAt(pgBody.PolygonBody.Points.Count - 1);

                pgBody.CommonProperty.Index = winCommonProp.tbIndex.Text;
                pgBody.CommonProperty.Content = winCommonProp.tbContent.Text;
                pgBody.CommonProperty.Detail = winCommonProp.tbDetail.Text;
                pgBody.CommonProperty.Floor = winCommonProp.cboFloor.Text;

                PolygonCollection.Add(pgBody);

                pgBody = ResourceCenter.NewPolygon();
                winPolygonProp.lstPointsList.Items.Clear();

                PolygonCanvas.Children.Add(pgBody.PolygonBody);

                BindListBox();
            }
            else
            {
                MessageBox.Show("aun no se ha completado el poligono!");
            }
        }

        private void MenuItem_DelLastPoint_Click(object sender, RoutedEventArgs e)
        {
            if (pgBody.PolygonBody.Points.Count > 2)
            {
                pgBody.PolygonBody.Points.RemoveAt(pgBody.PolygonBody.Points.Count - 1);

                BindListBox();
            }
            else
            {
                MessageBox.Show("debe quedar al menos un punto!");
            }
        }

        private void MenuItem_Redraw_Click(object sender, RoutedEventArgs e)
        {
            PolygonCanvas.Children.Remove(pgBody.PolygonBody);

            pgBody = ResourceCenter.NewPolygon();

            PolygonCanvas.Children.Add(pgBody.PolygonBody);
        }


        void PolygonBody_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            foreach (ASPNETX_WPFPolygon item in PolygonCollection)
            {
                item.PolygonBody.Fill = new SolidColorBrush(Colors.Blue);
            }

            Polygon mpolygon = (Polygon)sender;

            foreach (ASPNETX_WPFPolygon item in PolygonCollection)
            {
                if (item.PolygonBody == mpolygon)
                {
                    winPolygonProp.SetValue(item);
                    item.PolygonBody.Fill = new SolidColorBrush(Colors.Red);

                    winCommonProp.tbIndex.Text = item.CommonProperty.Index;
                    winCommonProp.tbContent.Text = item.CommonProperty.Content;
                }
            }
        }
    }
}